package com.yc.hwc.web;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.hwc.bean.Result;

@RestController
@RequestMapping("sys")
public class SysAction {

	@RequestMapping("pull")
	public Result<?> pull() {
		try {
			execWindowsCmd("/root/work/hwc/pull.sh");
			System.out.println("pull OK!");
			return Result.success("pull OK!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("pull Error!");
			return Result.failure("pull Error!");
		}
	}

	public void execWindowsCmd(String cmd) throws Exception {
		Runtime rt = Runtime.getRuntime();
		Process ppp = rt.exec(cmd);
		InputStreamReader ir = null;
		LineNumberReader input = null;
		String line;

		try {
			//input
			ir = new InputStreamReader(ppp.getInputStream());
			input = new LineNumberReader(ir);
			while ((line = input.readLine()) != null)
				System.out.println(line);
		} finally {
			IOUtils.closeQuietly(ir);
			IOUtils.closeQuietly(input);
		}

		try {
			//error
			ir = new InputStreamReader(ppp.getErrorStream());
			input = new LineNumberReader(ir);
			while ((line = input.readLine()) != null)
				System.err.println(line);
		} finally {
			IOUtils.closeQuietly(ir);
			IOUtils.closeQuietly(input);
		}
		ppp.waitFor();
	}
}
